#define DEBUG
#include <cstdio>
#define MAXN 100000

using namespace std;

class Ride {
public:
  int s, t;
};

void sort(int *a, int l, int r) {
  int i=l, j=r, mid=a[(l+r)/2], temp;
  for (; i<=j; ) {
    for (; a[j]>mid; j--);
    for (; a[i]<mid; i++);
    if (i<=j) {
      temp=a[i], a[i]=a[j], a[j]=temp;
      i++, j--;
    }
  }
  if (i<r) {
    sort(a, i, r);
  }
  if (j>l) {
    sort(a, l, j);
  }
}

int main() {
#ifdef DEBUG
  freopen("3187.in", "r", stdin);
  freopen("3187.out", "w", stdout);
#endif

  static int ls[MAXN+1], lt[MAXN+1], rs[MAXN+1], rt[MAXN+1], pos[2*MAXN+1], map[2*MAXN+1], sl[2*MAXN+1], sr[2*MAXN+1];
  int n, m, ln=0, rn=0, mn=0;
  scanf("%d %d", &n, &m);
  for (int i=1; i<=n; i++) {
    int s, t;
    scanf("%d %d", &s, &t);
    if (s<t) {
      ln++;
      ls[ln]=s, lt[ln]=t;
    } else {
      rn++;
      rs[rn]=s, rt[rn]=t;
    }
    pos[i*2-1] = s, pos[i*2] = t;
  }
  pos[n*2+1]=0, pos[n*2+2]=m;

  sort(pos, 1, n*2+2);
  map[++mn] = pos[1];
  for (int i=2; i<=n*2+2; i++) {
    if (pos[i]!=pos[i-1]) {
      map[++mn] = pos[i];
    }
  }

  sort(ls, 1, ln);
  for (int i=1, p=1; i<=ln; i++) {
    for (; map[p]<ls[i]; p++);
    sl[p+1]++;
  }

  sort(lt, 1, ln);
  for (int i=1, p=1; i<=ln; i++) {
    for (; map[p]<lt[i]; p++);
    sl[p+1]--;
  }

  sort(rs, 1, rn);
  for (int i=1, p=1; i<=rn; i++) {
    for (; map[p]<rs[i]; p++);
    sr[p+1]--;
  }

  sort(rt, 1, rn);
  for (int i=1, p=1; i<=rn; i++) {
    for (; map[p]<rt[i]; p++);
    sr[p+1]++;
  }

  int f=0, g=0;
  long long sum=0;
  for (int i=1; i<=mn; i++) {
    f = f+sl[i], g = g+sr[i];
    if (f || g) {
      if (f>g) {
	sum += (long long)(map[i]-map[i-1])*(2*f-1);
      } else {
	sum += (long long)(map[i]-map[i-1])*(2*g+1);
      }
    } else {
      sum += map[i]-map[i-1];
    }
  }

  printf("%lld", sum);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
